<?php
//including header.php 
include '../../include/header.php';
//including page.php (It is a class which contain functions)
include '../../libraries/page.php';
//created object for page.php
$page = new page();

session_start();

//if activation link request comes
if ($_GET['key']) {
    $code = $_GET['key'];
    $result = mysql_query("select * from users where randomcode = '$code'") or die(mysql_error());
    $user_data = mysql_fetch_array($result);
    $no_rows = mysql_num_rows($result) or die(mysql_error());
    if ($no_rows == 1) {
        if ($user_data['active'] === 'false') {
            //updating the database active field with 'true'.
            $result = mysql_query(
                    "UPDATE users SET active = 'true'
             WHERE randomcode = '$code'") or print 'There was an error: ' . mysql_error();
            //header("location:../../user/login");
        } else {
            $msg = $message[9];
        }
    } else {
        $msg = $message[10];
    }
}

//To get session
if ($page->get_session()) {
    header("location:index.php");
}

//If data posted by the form
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $login = $page->check_login($_POST['emailusername'], $_POST['password']);
    if ($login) {
// Login Success
        header("location:../groups/groups");
    } else {
        //Login Failed
        $msg = $message[7];
    }
}
include '../../include/navbar.php';
?>

<!----------------------------------Display the result message of Addition--------------------->
<div align="center"><? echo $msg; ?></div>

<!---------------------------------Login form----------------------------------------->
<div class="login_div">
    <form class="form-horizontal" method="POST" action="">
        <h2 style="color:#777">Login</h2>
        <div class="control-group">
            <label class="control-label" for="inputEmail">Email</label>
            <div class="controls">
                <input type="email" id="inputEmail" placeholder="Email" name="emailusername" required>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="inputPassword">Password</label>
            <div class="controls">
                <input type="password" id="inputPassword" placeholder="Password" name="password">
            </div>
        </div>
        <div class="control-group">
            <div class="controls">

                <a href="register">Sign up</a>
                <button type="submit" class="btn btn-inverse" style="margin-left: 95px">Sign in</button>
            </div>
        </div>
    </form>
</div>

<?php
//including footer.php
include '../../include/footer.php';
?>